package xyz.yuanjilu.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import xyz.yuanjilu.entity.BpmNodeRuleEntity;
import xyz.yuanjilu.service.BPMService;
import xyz.yuanjilu.utils.R;


/**
 * 流程相关配置控制
 * @author qiyang
 *
 */
@Controller
@RequestMapping("bpm/flow")
public class FlowController {
	
	@Autowired
	BPMService bpmService;
	
	/**
     * 获取流程图所有节点和连线
     * @param modelId 模型id
     */
    @RequestMapping("getModelNodeTree/{modelId}")
    @ResponseBody
    public R getModelNodeTree(@PathVariable String modelId) throws Exception {
        //所有节点
        List<Map<String, String>> flows = bpmService.getModelNode(modelId);
        R r=R.ok();
        r.put("treeData", flows);
        return r;
    }
    
    /**
     * 获取流程图所有节点和连线
     * @param modelId 模型id
     */
    @RequestMapping("getProcessDefinitionNodeTree/{processDefinitionId}")
    @ResponseBody
    public R getProcessDefinitionNodeTree(@PathVariable String processDefinitionId) throws Exception {
        //所有节点
        List<Map<String, String>> flows = bpmService.getProcessDefinitionNode(processDefinitionId);
        R r=R.ok();
        r.put("treeData", flows);
        return r;
    }
    /**
     * 获取流程图所有节点和连线
     * @param modelId 模型id
     */
    @RequestMapping("getModelNodeRuleTree/{modelId}/{nodeId}")
    @ResponseBody
    public R getModelNodeRuleTree(@PathVariable String modelId,@PathVariable String nodeId) throws Exception {
        //所有节点
    	List<BpmNodeRuleEntity> bpmNodeRuleEntities= bpmService.getModelNodeRule(modelId,nodeId);
        R r=R.ok();
        r.put("treeData", bpmNodeRuleEntities);
        return r;
    }
    
    /**
     * 获取流程图所有节点和连线
     * @param modelId 模型id
     */
    @RequestMapping("getProcessDefinitionNodeRuleTree/{processDefinitionId}/{nodeId}")
    @ResponseBody
    public R getProcessDefinitionNodeRuleTree(@PathVariable String processDefinitionId,@PathVariable String nodeId) throws Exception {
        //所有节点
    	List<BpmNodeRuleEntity> bpmNodeRuleEntities= bpmService.getProcessDefinitionNodeRule(processDefinitionId,nodeId);
        R r=R.ok();
        r.put("treeData", bpmNodeRuleEntities);
        return r;
    }
    /**
     * 查看流程图片
     * @param modelId
     * @return
     */
    @RequestMapping(value = "showModelFlowImg/{modelId}")
    public ResponseEntity<byte[]> showModelFlowImg(@PathVariable String modelId){
        return bpmService.showModelFlowImg(modelId);
    }
    
    /**
     * 部署流程的配置信息
     * @param modelId 模型id
     */
    @RequestMapping("deploymentModelRule/{modelId}/{deploymentId}")
    @ResponseBody
    public R deploymentModelRule(@PathVariable String modelId,@PathVariable String deploymentId) throws Exception {
    	bpmService.deploymentModelRule(modelId, deploymentId);
        R r=R.ok();
        return r;
    }
    /**
     * 获取流程按钮
     * @param modelId 模型id
     */
    @RequestMapping("getRuntimeTaskButton/{processDefinitionId}/{nodeId}")
    @ResponseBody
    public R getRuntimeTaskButton(@PathVariable String processDefinitionId,@PathVariable String nodeId) throws Exception {
    	List<BpmNodeRuleEntity> runtimeTaskButton = bpmService.getRuntimeTaskButton(processDefinitionId, nodeId);
    	R r=R.ok();
    	r.put("data", runtimeTaskButton);
    	return r;
    }
    /**
     * 获取流程按钮的规则
     * @param modelId 模型id
     */
    @RequestMapping("getRuntimeTaskButtonRule/{ruleId}")
    @ResponseBody
    public R getRuntimeTaskButtonRule(@PathVariable String ruleId) throws Exception {
    	List<BpmNodeRuleEntity> runtimeTaskButton = bpmService.getRuntimeTaskButtonRule(ruleId);
    	R r=R.ok();
    	r.put("data", runtimeTaskButton);
    	return r;
    }
}
